/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tallerfinal.util.distributions;

import tallerfinal.util.distributions.factory.GeneratorsFactory;
import tallerfinal.util.enums.DistributionsName;

/**
 *
 * @author ricardo
 */
public class PoissonGenerator extends Generator<Integer> {
static {
        GeneratorsFactory.getInstance().registerProduct(DistributionsName.POISSON, PoissonGenerator.class);
    }
    private double lambda;

    public PoissonGenerator(double lambda) {
        this.lambda = lambda;
    }

    @Override
    public Integer generate() {
        double L = Math.exp(-lambda);
        double p = 1.0;
        int k = 0;

        do {
            k++;
            p *= generator.nextDouble();
        } while (p > L);

        return k - 1;

    }
}
